layui.use(['layer','form','jquery','laydate','upload','element'],function () {
    var form = layui.form;
    var layer = layui.layer;
    var $ = layui.$;
    var laydate = layui.laydate;
    var upload = layui.upload;
    var element = layui.element;
    var bannerCreate = JSON.parse(localStorage.getItem('user'));
    $('#createId').val(bannerCreate.data.userId);
    //返回功能
    $('#goBack').click(function () {
        parent.tabParentChange(3);
    })
    //刷新功能
    $('#refresh').click(function () {
        location.reload();
    });
    //开始时间 结束时间渲染
    laydate.render({
        elem: '#expireDate',
        type: 'datetime'
    });
    laydate.render({
        elem: '#startDate',
        type: 'datetime'
    });
    var imgFiles;
    var imgIndex;
    var imgUploadCount = 0;
    //上传图片
    upload.render({
        elem: '#uploadBannerImg',
        accept: 'images',
        size: 2048,
        field: 'image',
        url: '/banner/uploadBannerImage',
        choose: function(obj){
            imgUploadCount++;
            obj.preview(function(index, file, result){
                $('#bannerImg').attr('src',result);
                imgIndex = index;
            });
            if(imgUploadCount != 1){
                delete imgFiles[imgIndex];
            }
            imgFiles = obj.pushFile();
            element.progress('imgProgress', '99%');
        },
        done: function (res,index,upload) {
            if(res.data.code == 200){
                $('#imgAddress').val(res.data.src);
                layer.msg('上传图片成功');
                element.progress('imgProgress', '100%');
            }
        }
    });
    var videoFiles;
    var videoIndex;
    var videoUploadCount = 0;
    //上传视频
    upload.render({
        elem: '#uploadBannerViedo',
        accept: 'video',
        size: 409600 ,
        field: 'video',
        url: '/banner/uploadBannerVideo',
        choose: function(obj){
            videoUploadCount++;
            obj.preview(function(index, file, result){
                videoIndex = index;
                $('#bannerViedo').attr('src',result);
            });
            if(videoUploadCount != 1){
                delete videoFiles[videoIndex];
            }
            videoFiles = obj.pushFile();
            element.progress('videoProgress', '99%');
        },
        done: function (res,index,upload) {
            if(res.data.code == 200){
                $('#videoAddress').val(res.data.src);
                layer.msg('上传视频成功');
                element.progress('videoProgress', '100%');
            }
        }
    })
    //图片视频输入框验证
    form.verify({
        imgAddress: function (value, item) {
            if(value == ''){
                return '请先上传图片';
            }
        },
        videoAddress: function (value, item) {
            if(value == ''){
                return '请先上传视频';
            }
        }
    })
    //添加轮播图
    form.on('submit(sub)',function (obj) {
        $.ajax({
            url: '/banner/addBanner',
            type: 'post',
            dataType: 'json',
            data: obj.field,
            success: function (data) {
                if (data.data.carouselId != null){
                    location.reload();
                    parent.addTab(56);
                }
            }
        });
        return false;
    })
})